// mypage/editBaby/editBaby.js
var app=getApp();
var other = require("../../utils/other");
var util=require("../../utils/util");
var config = require("../../api/config");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    info: null,
    avatar: '',
    name: '',
    sex: '',
    birthday: '',
    age_step: '',
    grade: '',
    title_index: null,
    item_index: null,
    id: '',
    del_pop: true,
    pop_show: true,
    show_com: 0,
    com_data: ["男","女"],
    grade_list: [],
    pop_page: null,
    edit: true,
    default_img: ["http://ossdev0.jiabala.com/tnr5Y8kcKzNdR","http://ossdev0.jiabala.com/jjDPpzQfmThK3"]
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({id:id})
    //
    that.get_baby_detail(id);
  },
  // 上传头像
  choose_image:function(e){
    var that = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['album', 'camera'],
      success: (res) => {
        // console.log(res)
        wx.showLoading({
          title: '上传中'
        })
        that.get_oss(res)
      },
    })
  },
  // 名字
  input_name:function(e){
    var that=this;
    var value=e.detail.value;
    if(value.length>10){
      util.page_show_toast('宝宝昵称不能超过10个字符',1500)
    }
    that.setData({name:value.substring(0,10)});
  },
  // 选择
  open_addpop:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    if(index==1){
      that.setData({pop_show: !that.data.pop_show,show_com: index,com_data: ["男","女"],selectValue:that.data.sex-1,sex_value:[that.data.sex-1]})
    }else if(index==2){
      var obj = other.dateTimePicker(2000,new Date().getFullYear())
      that.setData({com_data: obj.dateTimeArray,pop_show: !that.data.pop_show,show_com: index})
    }else if(index==3){
      that.setData({com_data:that.data.grade_list,pop_show: !that.data.pop_show,show_com: index})
    }
  },
  close_addpop:function(e){
    var that = this;
    var obj = e.detail.pop_show;
    that.setData({pop_show: obj})
  },
  picker_change:function(e){
    var that = this;
    var index = e.detail.index;
    that.setData({selectValue:index,sex_value:[index]})
  },
  check_sex:function(e){
    var that = this,info=that.data.info,default_img=that.data.default_img,avatar=info.avatar;
    var sex = e.detail.sex=='男'?1:2;
    if(info.avatar==default_img[info.sex-1]){
      avatar=default_img[sex-1]
    }
    that.setData({sex:sex,avatar:avatar})

  },
  change_time:function(e){
    this.setData({com_data: e.detail.time})
  },
  confirm_age:function(e){
    var that = this;
    that.setData({birthday: e.detail.age})
  },
  //
  check_inst:function(e){
    var that = this;
    var info=e.detail.data;
    that.setData({title_index:info.title_index,item_index:info.item_index})
  },
  confirm_inst:function(e){
    var that = this;
    that.setData({grade: e.detail.item})
  },
  // 修改宝宝信息
  confirm_baby:function(){
    var that=this,avatar=that.data.avatar,name=that.data.name,sex=that.data.sex,birthday=that.data.birthday,grade=that.data.grade,info=that.data.info,testSpace = /^[ ]*$/g,edit=that.data.edit;
    if(avatar!=info.avatar||name!=info.name||sex!=info.sex||birthday!=info.birthday||grade!=info.grade){
      if(testSpace.test(name)||!name){
        util.page_show_toast('请输入规范昵称',1500)
        return false;
      }
      if(!grade){
        util.page_show_toast('未选择宝宝在读年纪',1500)
        return false;
      }
      var params={
        "id": that.data.id,
        "name":util.removeAllSpace(name),
        "phone": "",
        "birthday": Date.parse(birthday),
        "sex": sex,
        "grade": grade,
        "avatar": avatar
      }
      if(edit){
        //
        that.edit_baby(params)
      }
    }else{
      wx.navigateBack({
        delta: 1
      });
    }
  },
  // 删除
  open_pop:function(){
    var that=this,id=that.data.id;
    //
    that.judeg_del_baby(id)
  },
  close_pop:function(){
    this.setData({del_pop: !this.data.del_pop})
  },
  confirm_del:function(){
    var that=this,id=that.data.id
    //
    that.del_baby(id);
  },
  //
  onReady:function(){
    this.dialog = this.selectComponent('#addpop');
  },
  // 获取详情
  get_baby_detail:function(id){
    var that=this;
    my.get_baby_detail({id:id}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({info:info,avatar:info.avatar,name:info.name,sex:info.sex,birthday:util.formatTime_age(new Date(info.birthday)),grade:info.grade})
        //
        that.get_grade_list();
      }
    })
  },
  // 修改请求
  edit_baby:function(params){
    var that=this;
    that.setData({edit: false})
    my.edit_baby(params).then(res=>{
      if(res.data.code==200){
        app.globalData.change_baby=true;
        util.page_show_toast('修改成功',1500)
        setTimeout(function(){
          that.setData({edit: true})
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }
    })
  },
  // 删除宝宝-判断是否购买课程
  judeg_del_baby:function(id){
    var that=this;
    my.judeg_del_baby({"childId":id}).then(res=>{
      if(res.data.code==200){
        that.setData({pop_page:res.data.data,del_pop: !that.data.del_pop})
      }
    })
  },
  // 删除宝宝
  del_baby:function(id){
    var that=this;
    my.del_baby({id: id}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        app.globalData.change_baby=true;
        app.globalData.change_table=true;
        that.close_pop();
        util.page_show_toast('删除成功',1500)
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }
    })
  },
   // 在读年纪列表
   get_grade_list:function(){
    var that=this;
    config.get_grade_list().then(res=>{
      if(res.data.code==200){
        var list=res.data.data,grade=that.data.grade,title_index=0,item_index=0;
        if(grade){
          for(var i=0;i<list.length;i++){
            var gradeList=list[i].gradeList;
            for(var j=0;j<gradeList.length;j++){
              if((gradeList[j].text).indexOf(grade)>-1){
                title_index=i;
                item_index=j;
              }
            }
          }
        }
        that.setData({grade_list:list,title_index:title_index,item_index:item_index})
      }
    })
  },
  // 获取oss上传图片
  get_oss:function(file){
    var that=this,randomStr=util.randomString(13);
    var params={
      "isPublic": true,
      "isHttps": true
    }
    config.get_oss(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        wx.uploadFile({
          url: info.host,
          filePath: file.tempFilePaths[0],
          name: 'file',
          formData: {
            "key": randomStr,
            "policy": info.policy,
            "OSSAccessKeyId": info.accessKeyId,
            "signature": info.signature,
            "success_action_status": '200',

          },
          success:function(){
            wx.hideLoading()
            var avatar=info.host+'/'+randomStr;
            console.log(avatar)
            that.setData({avatar: avatar})
          }
        })
      }
    })
  },
})